# -*- coding: utf-8 -*-
import json
import os

from compare_module.bean_code_parse import get_class_members
from compare_module.controller_code_parse import find_controller_files, compare_controller_params
from compare_module.diff_type import DiffType
from compare_module.export_csv import append_export_data
from compare_module.export_csv import export_csv



#比较两个代码文件夹
def compare_dir(args):
    controller_files1, all_files1 = find_controller_files(args[0])
    controller_files2, all_files2 = find_controller_files(args[1])

    new_controller_names = []
    new_method_names = []
    diff_types = []
    diff_infos = []
    export_data = {
        "Controller名称": new_controller_names,
        "方法名称": new_method_names,
        "不兼容类型": diff_types,
        "详细信息": diff_infos
    }

    for file1 in controller_files1:
        for file2 in controller_files2:
            if os.path.basename(file1) == os.path.basename(file2):
                # 比较controller方法参数
                result  = compare_controller_params(file1, file2,export_data)
                if None == result:
                    continue
                type_names, controllerA, controllerB, new_method_name = result

                # 比较controller参数中的实体类
                modles1 = []
                modles2 = []
                for type_name in type_names:
                    for file_a in all_files1:
                        if os.path.basename(file_a) == (type_name + ".java"):
                            modles1.append(file_a)
                    for file_b in all_files2:
                        if os.path.basename(file_b) == (type_name + ".java"):
                            modles2.append(file_b)
                for modles_a in modles1:
                    for modles_b in modles2:
                        if os.path.basename(modles_a) == os.path.basename(modles_b):
                            class_members_a = get_class_members(modles_a)
                            class_members_b = get_class_members(modles_b)
                            for key_a in class_members_a:
                                for key_b in class_members_b:
                                    if key_a == key_b:
                                        ##[param for param in param_names1 if param not in param_names]
                                        delete_fields = [param for param in class_members_a[key_a] if
                                                         param not in class_members_b[key_a]]
                                        if delete_fields:
                                            append_export_data(export_data,controllerB,new_method_name,DiffType.METHOD_MODELS,json.dumps(delete_fields))
                                            continue
    export_csv(export_data)